import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { addBuilder } from "store/dict";
import * as services from "services/Lzk_services/lzkservers";

//病友圈tab数据
export const patientTab = createAsyncThunk<any>(
  "patient/patientTab",
  async () => {
    const resp = services.patintTabs();
    return resp;
  }
);

//病友圈页面数据
export const patientList = createAsyncThunk<any, any>(
  "patient/patientList",
  async (data) => {
    const resp = services.patintList(data);
    return resp;
  }
);

//病友圈建议
export const sugGetion = createAsyncThunk<any, any>(
  "patient/sugGetion",
  async (id) => {
    const resp = services.suggetion(id);
    return resp;
  }
);

//写解答
export const myAnswer = createAsyncThunk<any, any>(
  "patient/myAnswer",
  async (data) => {
    const resp = services.myanswer(data);
    return resp;
  }
);

//注册
export const register = createAsyncThunk<any, any>(
  "patient/register",
  async (data) => {
    const resp = services.register(data);
    return resp;
  }
);

//获取验证码
export const getcode = createAsyncThunk<any, any>(
  "patient/getcode",
  async (email) => {
    const resp = services.getcode(email);
    return resp;
  }
);

//历史搜索

const initialState: any = {};

const patient = createSlice({
  name: "patient",
  initialState,
  reducers: {},
  extraReducers: (buildr) => {
    //病友圈tab数据
    addBuilder(buildr, patientTab, (state, action) => {
      const { result } = action.payload || {};
      if (result) {
        state.patientTabs = result;
      }
    });

    //病友圈页面数据
    addBuilder(buildr, patientList, (state, action) => {
      //   console.log(action);
      const { result } = action.payload || {};
      if (result) {
        state.patientList = result;
      }
    });

    //病友圈建议
    addBuilder(buildr, sugGetion, (state, action) => {
      const { result } = action.payload || {};
      if (result) {
        state.sugList = result;
      }
    });
    //写解答
    addBuilder(buildr, myAnswer, (state, action) => {});

    //注册
    addBuilder(buildr, register, (state, action) => {});

    //获取验证码
    addBuilder(buildr, getcode, (state, action) => {});

    // //历史搜索
    // addBuilder(buildr, listory, (state, action) => {
    //   console.log(action);
    // });
  },
});

export default patient.reducer;

//病友圈tab数据
export const patientTabs = (globalState: any) => {
  return globalState.patient.patientTabs;
};

//病友圈页面数据
export const patientlist = (globalState: any) => {
  return globalState.patient.patientList;
};

//病友圈建议
export const suggetionList = (globalState: any) => {
  return globalState.patient.sugList;
};
